<!--
 * @Description: 开通还款通道/开通用户结清证明
 * @Author gong
 * @create 2024/9/5 14:42
 -->
<!--setup-->
<script setup>
import { onMounted, getCurrentInstance, ref } from 'vue'
import { BaseFormItem, BaseForm } from '@shared/base/base-form' // 表单公共组件
import { to } from 'sf-utils2'
import corePrjInfoApi from '@/api/modules/core-prj-info' // api接口
import { ElMessage } from 'element-plus'

const { proxy } = getCurrentInstance()
const props = defineProps({
  // 配置类型  还款：10/结清：20
  configType: {
    type: String,
    default: () => ''
  },
  // 项目id
  prjId: {
    type: String,
    default: () => ''
  },
  // 项目名称
  prjName: {
    type: String,
    default: () => ''
  },
  // 项目编码
  prjCode: {
    type: String,
    default: () => ''
  },
  // 核心业务编码
  coreCode: {
    type: String,
    default: () => ''
  }
})
const emit = defineEmits([])

/* 状态 */
const formData = ref({})
const baseFormRef = ref() // base-form 实例

/* 方法 */
/**
 * 初始化数据
 */
const init = async () => {
  const params = {
    prjId: props.prjId, // 项目id
    configType: props.configType // 配置类型
  }
  const [res, err] = await to(corePrjInfoApi.getCorePrjInfoDetail(params))
  if (err || res?.code !== 200) return ElMessage.error(res?.message || String(err))
  formData.value = {
    ...res.data,
    prjName: props.prjName, // 项目名称
    prjCode: props.prjCode // 项目编码
  }
  // 设置初始值，避免刚进页面时校验
  formData.value.showName ||= ''
  formData.value.corePrjNo ||= ''

  // 核心业务代码/
  if (props.coreCode && !props.coreCode.includes(',')) {
    formData.value.corePrjNo ||= props.coreCode
  }
}

/**
 * 校验并返回数据
 */
const onSave = async () => {
  const valid = await baseFormRef.value.validate()
  if (valid) {
    const params = {
      ...formData.value,
      configType: props.configType, // 配置类型
      prjId: props.prjId // 项目id
    }
    const [res, err] = await to(corePrjInfoApi.editCorePrjConfigSwitch(params))
    if (err || res?.code !== 200) {
      ElMessage.error(res?.message || String(err))
      return false
    }
    return res
  }
}

/* 计算 */

/* 监听 */

/* 周期 */
onMounted(() => {
  init()
})

/* 暴露 */
defineExpose({
  $: proxy.$,
  onSave
})
</script>

<!--render-->
<template>
  <BaseForm
    ref="baseFormRef"
    v-model="formData"
    label-position="top"
  >
    <BaseFormItem
      label="项目名称"
      prop="prjName"
      type="input"
      disabled
      :col="24"
    />
    <BaseFormItem
      label="项目编码"
      prop="prjCode"
      type="input"
      disabled
      :col="24"
    />
    <BaseFormItem
      label="客户端展示名称"
      prop="showName"
      type="input"
      required
      maxlength="200"
      :component-attrs="{ showWordLimit: false }"
      :col="24"
    />
    <BaseFormItem
      label="核心业务编码"
      prop="corePrjNo"
      type="input"
      required
      maxlength="200"
      :component-attrs="{ showWordLimit: false }"
      :col="24"
    />
    <BaseFormItem
      label="转让日期"
      prop="transferDate"
      type="date"
      required
      :col="24"
    />
  </BaseForm>
</template>

<!--style-->
<style scoped lang="less"></style>
